/**
 * 
 */
package com.onlinemathuracrud.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.onlinemathuracrud.configurations.ApplicationConstants;
import com.onlinemathuracrud.dao.CityDao;
import com.onlinemathuracrud.dao.TempleDao;
import com.onlinemathuracrud.model.Image;
import com.onlinemathuracrud.model.Temple;
import com.onlinemathuracrud.service.TempleService;

/**
 * @author Amit Kashyap
 * @createdDate Sep 7, 2013
 * @createdTime 1:53:52 AM
 */
@Controller
@RequestMapping(value = "/temple")
public class TempleCrudController {

	@Autowired
	TempleDao templeDao;
	
	@Autowired
	CityDao cityDao;
	
	@Autowired
	TempleService templeService;
	
	@RequestMapping(value = "/showtemples", method = RequestMethod.GET)
	public String showTemples(ModelMap model, @ModelAttribute("templedata") Temple temple) {

		model.addAttribute("allTemples", templeDao.findAll());
		model.addAttribute("cities",cityDao.findAll());
	//	templeDao.printStatics();
		return "temples";
	}

	@RequestMapping(value = "/edittemple/{id}", method = RequestMethod.GET)
	public String edittemple(ModelMap model, @PathVariable("id") Long templeId) {

		model.addAttribute("templedata", templeDao.findById(templeId));
		model.addAttribute("cities",cityDao.findAll());
		model.addAttribute("allTemples", templeDao.findAll());
		return "temples";
	}

	@RequestMapping(value = "/deletetemple/{id}", method = RequestMethod.GET)
	public String deletetemple(ModelMap model,
			@PathVariable("id") Long templeId,
			@ModelAttribute("templedata") Temple temple) {

		templeDao.delete(templeDao.findById(templeId));
		return "redirect:/temple/showtemples";
	}

	@RequestMapping(value = "/savetemple", method = RequestMethod.POST)
	public String savetemple(ModelMap model,Temple temple,HttpServletRequest request)  {
			templeService.save(temple);
			return "redirect:/temple/showtemples";
	}

}
